<!DOCTYPE html>
<html lang="zh-Hans">
<head>

    <!--[if lt IE 9]>
        <style>body {display: none; background: none !important} </style>
        <meta http-equiv="Refresh" Content="0; url=//outdatedbrowser.com/" />
    <![endif]-->

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="format-detection" content="telephone=no" />
<meta name="author" content="zhengweiMao" />



<meta property="og:type" content="article">
<meta property="og:title" content="3.SpringMVC -初始化源码分析">
<meta property="og:url" content="https://maozw.gitee.io/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/index.html">
<meta property="og:site_name" content="花神">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://ohfk1r827.bkt.clouddn.com/spring-mvc1.jpg">
<meta property="og:updated_time" content="2018-08-06T15:22:03.361Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="3.SpringMVC -初始化源码分析">
<meta name="twitter:image" content="http://ohfk1r827.bkt.clouddn.com/spring-mvc1.jpg">

<link rel="apple-touch-icon" href= "/apple-touch-icon.png">


    <link rel="alternate" href="/atom.xml" title="花神" type="application/atom+xml">



    <link rel="shortcut icon" href="/favicon.png">



    <link href="//cdn.bootcss.com/animate.css/3.5.1/animate.min.css" rel="stylesheet">



    <link href="//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet">



    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-minimal.css" rel="stylesheet">


<link rel="stylesheet" href="/maoge-blog/css/style.css">



<link href="//cdn.bootcss.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet">


<title>3.SpringMVC -初始化源码分析 | 花神</title>

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<script>
    var yiliaConfig = {
        fancybox: true,
        animate: true,
        isHome: false,
        isPost: true,
        isArchive: false,
        isTag: false,
        isCategory: false,
        fancybox_js: "//cdn.bootcss.com/fancybox/2.1.5/jquery.fancybox.min.js",
        scrollreveal: "//cdn.bootcss.com/scrollReveal.js/3.1.4/scrollreveal.min.js",
        search: true
    }
</script>


    <script>
        yiliaConfig.jquery_ui = [true, "//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js", "//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css"];
    </script>



    <script> yiliaConfig.rootUrl = "\/";</script>






</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/maoge-blog/" class="profilepic">
            <img src="img/avatar.png" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/maoge-blog/">zhengweiMao</a></h1>
        </hgroup>

        

        
            <form id="search-form">
            <input type="text" id="local-search-input" name="q" placeholder="search..." class="search form-control" autocomplete="off" autocorrect="off" searchonload="" />
            <i class="fa fa-times" onclick="resetSearch()"></i>
            </form>
            <div id="local-search-result"></div>
            <p class='no-result'>No results found <i class='fa fa-spinner fa-pulse'></i></p>
        


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/maoge-blog/">主页</a></li>
                        
                            <li><a href="/maoge-blog/archives/">所有文章</a></li>
                        
                            <li><a href="/maoge-blog/tags/">标签云</a></li>
                        
                            <li><a href="/maoge-blog/categories/">分类</a></li>
                        
                            <li><a href="/maoge-blog/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" href="/maoge-blog/atom.xml" title="RSS"></a>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
                    
                      <a class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">专注于前端</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">zhengweiMao</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/maoge-blog/" class="profilepic">
                <img src="img/avatar.png" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/maoge-blog/" title="回到主页">zhengweiMao</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/maoge-blog/">主页</a></li>
                
                    <li><a href="/maoge-blog/archives/">所有文章</a></li>
                
                    <li><a href="/maoge-blog/tags/">标签云</a></li>
                
                    <li><a href="/maoge-blog/categories/">分类</a></li>
                
                    <li><a href="/maoge-blog/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <a class="fa Email" target="_blank" href="mailto:123@123.com" title="Email"></a>
                            
                                <a class="fa GitHub" target="_blank" href="#" title="GitHub"></a>
                            
                                <a class="fa RSS" target="_blank" href="/maoge-blog/atom.xml" title="RSS"></a>
                            
                        </ul>
            </nav>
        </header>                
    </div>
    <link class="menu-list" tags="标签" friends="友情链接" about="关于我"/>
</nav>
      <div class="body-wrap"><article id="post-花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/" class="article-date">
      <time datetime="2018-07-12T07:38:05.000Z" itemprop="datePublished">2018-07-12</time>
</a>


    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      3.SpringMVC -初始化源码分析
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
    <div class="article-category tagcloud">
    <a class="article-category-link" href="/maoge-blog/categories/源码分析-SpringMVC/">源码分析-SpringMVC</a>
    </div>


        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <p><excerpt in="" index="" |="" 首页摘要=""><br><a id="more"></a></excerpt></p>
<the rest="" of="" contents="" |="" 余下全文="">

<h2 id="SpringMVC—–源码分析与实践"><a href="#SpringMVC—–源码分析与实践" class="headerlink" title="SpringMVC—–源码分析与实践"></a>SpringMVC—–源码分析与实践</h2><p>本系列文章采用工具如下</p>
<blockquote>
<ol>
<li>idea </li>
<li>MAVEN</li>
</ol>
</blockquote>
<h6 id="序言"><a href="#序言" class="headerlink" title="序言"></a>序言</h6><h4 id="SpringMVC"><a href="#SpringMVC" class="headerlink" title="SpringMVC"></a>SpringMVC</h4><hr>
<h3 id="SpringMVC—–源码分析与实践-初始化分析"><a href="#SpringMVC—–源码分析与实践-初始化分析" class="headerlink" title="SpringMVC—–源码分析与实践-初始化分析"></a>SpringMVC—–源码分析与实践-初始化分析</h3><ul>
<li><ol>
<li>demo工程</li>
</ol>
</li>
<li><ol start="2">
<li>启动分析</li>
</ol>
</li>
<li><ol start="3">
<li>使用分析</li>
</ol>
</li>
</ul>
<hr>
<h2 id="一-启动分析"><a href="#一-启动分析" class="headerlink" title="一  启动分析"></a>一  启动分析</h2><p>Spring MVC 初始化源码分析</p>
<h3 id="Spring-MVC-核心-Servlet-架构图如下："><a href="#Spring-MVC-核心-Servlet-架构图如下：" class="headerlink" title="Spring MVC 核心 Servlet 架构图如下："></a>Spring MVC 核心 Servlet 架构图如下：</h3><p><img src="http://ohfk1r827.bkt.clouddn.com/spring-mvc1.jpg" alt="image"></p>
<blockquote>
<p>SpringMVC也是基于servlet实现。本文不再介绍servlet;</p>
<ul>
<li>springmvc 我先从使用开始一步一步解析其初始化过程，springmvc的核心是DispatcherServlet，它是前端控制器，负责拦截客户端发过来的请求，然后解析请求进行分发。 </li>
<li>DispatcherServlet是基于Servlet的，所以使用springmvc先在web.xml中配置DispatcherServlet；</li>
<li>在web.xml配置了一个名叫DispatcherServlet的Servlet，所有的请求都会在它那里执行，并且它有一个名叫contextConfigLocation的参数，在这里赋值为classpath:springMVC.xml</li>
<li>DispatcherServlet本质还是一个Servlet，web容器启动时会会加载DispatcherServlet，Servlet在第一次加载时都会调用其 init() 方法，但是DispatcherServlet本身没有这个方法，所以系统会去它父类寻找init（）方法，最后在HttpServletBean找到，调用。</li>
</ul>
</blockquote>
<h5 id="这里主要阐述Spring-中的-HttpServletBean、FrameworkServlet、DispatcherServlet-这三个类的创建过程。"><a href="#这里主要阐述Spring-中的-HttpServletBean、FrameworkServlet、DispatcherServlet-这三个类的创建过程。" class="headerlink" title="这里主要阐述Spring 中的 HttpServletBean、FrameworkServlet、DispatcherServlet 这三个类的创建过程。"></a>这里主要阐述Spring 中的 HttpServletBean、FrameworkServlet、DispatcherServlet 这三个类的创建过程。</h5><p>通过上面的图，可以看到这三个类直接实现三个接口：EnvironmentCapable、EnvironmentAware、ApplicationContextAware。下面我们直接看下这三个接口的内部是怎样写的。</p>
<ul>
<li style="list-style: none"><input type="checkbox" checked> EnvironmentCapable.java</li>
<li>下面是源代码 代码注释是自己添加的 原文没有注释<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.springframework.core.env;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">EnvironmentCapable</span> </span>&#123;</span><br><span class="line">    <span class="comment">//返回组件的环境，可能返回 null 或者默认环境</span></span><br><span class="line">    <span class="function">Environment <span class="title">getEnvironment</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<ul>
<li style="list-style: none"><input type="checkbox" checked> EnvironmentAware<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.Aware;</span><br><span class="line"><span class="keyword">import</span> org.springframework.core.env.Environment;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">EnvironmentAware</span> <span class="keyword">extends</span> <span class="title">Aware</span> </span>&#123;</span><br><span class="line">    <span class="comment">//设置组件的运行环境</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">setEnvironment</span><span class="params">(Environment var1)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<hr>
<ul>
<li style="list-style: none"><input type="checkbox" checked> ApplicationContextAware</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.springframework.beans.BeansException;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.Aware;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">ApplicationContextAware</span> <span class="keyword">extends</span> <span class="title">Aware</span> </span>&#123;</span><br><span class="line">    <span class="comment">//设置运行对象的应用上下文</span></span><br><span class="line">    <span class="comment">//当类实现这个接口后，这个类可以获取ApplicationContext中所有的bean，也就是说这个类可以直接获取Spring配置文件中所有有引用到的bean对象</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">setApplicationContext</span><span class="params">(ApplicationContext var1)</span> <span class="keyword">throws</span> BeansException</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li><strong>这个接口有什么用</strong>？<blockquote>
<p>一个类实现了这个接口（ApplicationContextAware）之后，这个类就可以方便获得ApplicationContext中的所有bean。换句话说，就是这个类可以直接获取spring配置文件中，所有有引用到的bean对象。</p>
</blockquote>
</li>
<li>怎么用？<blockquote>
<p>举个例子吧：例如我有一个方法类AppUtil，这个方法类中需要使用到的ApplicationContext中的某个bean（xxxService）</p>
<ol>
<li>因为spring要建立属于自己的容器，就必须要加载自己的配置文件。这个时候，需要注册ContextLoaderListener或者这个类的子类。在web.xml加上以下的信息()：<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">listener</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">listener-class</span>&gt;</span>org.springframework.web.context.ContextLoaderListener<span class="tag">&lt;/<span class="name">listener-class</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">listener</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
</blockquote>
</li>
</ul>
<p>当然，这样子的话只会读取默认路径下的application.xml配置文件的。如果需要读取特定路径下的配置文件。需要在web.xml中添加如下信息。指定配置文件，如下：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context-param</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>classpath:conf/applicationcontext.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">context-param</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>注意：<param-name>contextConfigLocation</param-name>是不能改变的。</p>
<blockquote>
<ol start="2">
<li>方法类AppUtil: AppUtil实现ApplicationContextAware接口：为方法类AppUtil增加一个静态的成员ApplicationContext类型的对象。以后方法类AppUtil获取ApplicationContext，就是通过读取这个成员变量的。具体如下所示：</li>
</ol>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AppUtil</span> <span class="keyword">implements</span> <span class="title">ApplicationContextAware</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> ApplicationContext appContext;</span><br><span class="line">    <span class="comment">//实现ApplicationContextAware接口的默认方法：</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setApplicationContext</span><span class="params">(ApplicationContext paramApplicationContext)</span> <span class="keyword">throws</span> BeansException </span>&#123;</span><br><span class="line">        appContext = paramApplicationContext;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<ol start="3">
<li>在spring的配置文件中，注册方法类AppUtil<br>严格上来说，方法类AppUtil是一个bean,而且从步骤2中我们不难发现，之所以方法类AppUtil能够灵活自如地获取ApplicationContext,就是因为spring能够为我们自动地执行了setApplicationContext。但是，spring不会无缘无故地为某个类执行它的方法的，所以，就很有必要通过注册方法类AppUtil的方式告知spring有这样子一个类的存在。方法很简单，就是将方法类AppUtil作为一个普通的bean在spring的配置文件中进行注册：(后续会说明通过注解的方式 更为简便)<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"appUtil"</span> <span class="attr">class</span>=<span class="string">"com.htsoft.core.util.AppUtil"</span>/&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
</blockquote>
<blockquote>
<ol start="4">
<li>使用静态的成员ApplicationContext类型的对象,appContext，来调用其他bean。在方法类AppUtil中增加如下方法:<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Object <span class="title">getBean</span><span class="params">(String paramString)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> appContext.getBean(paramString);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ol>
</blockquote>
<p>那么，在方法类AppUtil中就能够灵活地调用其他任何一个bean了，例如：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">XXXService xxxService = (XXXService)getBean(<span class="string">"xxxService"</span>);</span><br></pre></td></tr></table></figure></p>
<h6 id="注意：XXXService也是Spring所托管的bean。"><a href="#注意：XXXService也是Spring所托管的bean。" class="headerlink" title="注意：XXXService也是Spring所托管的bean。"></a>注意：XXXService也是Spring所托管的bean。</h6><hr>
<h4 id="好了言归正传，说明了上面三个接口之后，该说我们的正主了；"><a href="#好了言归正传，说明了上面三个接口之后，该说我们的正主了；" class="headerlink" title="好了言归正传，说明了上面三个接口之后，该说我们的正主了；"></a>好了言归正传，说明了上面三个接口之后，该说我们的正主了；</h4><ul>
<li style="list-style: none"><input type="checkbox" checked> HttpServletBean</li>
<li>由于篇幅过长，无关紧要的方法我就省略了 不做解释</li>
<li>这里直接看init方法:为什么要看她 上面已经阐述了，因为web容器加载DispacterServlet时会执行init方法；<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">HttpServletBean</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> <span class="keyword">implements</span> <span class="title">EnvironmentCapable</span>, <span class="title">EnvironmentAware</span> </span>&#123;</span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">final</span> Log logger = LogFactory.getLog(<span class="keyword">this</span>.getClass());</span><br><span class="line">    <span class="keyword">private</span> ConfigurableEnvironment environment;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Set&lt;String&gt; requiredProperties = <span class="keyword">new</span> HashSet(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">HttpServletBean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> ConfigurableEnvironment <span class="title">createEnvironment</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> StandardServletEnvironment();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 将配置参数映射到此servlet的bean属性，并调用子类初始化。</span></span><br><span class="line"><span class="comment">     * 如果 bean 配置不合法（或者需要的参数丢失）或者子类初始化发生错误，那么就会抛出 ServletException 异常</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span> <span class="keyword">throws</span> ServletException </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isDebugEnabled()) &#123;</span><br><span class="line">            <span class="keyword">this</span>.logger.debug(<span class="string">"Initializing servlet '"</span> + <span class="keyword">this</span>.getServletName() + <span class="string">"'"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 从init参数设置bean属性。</span></span><br><span class="line">        <span class="comment">// 获得web.xml中的contextConfigLocation配置属性，就是spring MVC的配置文件</span></span><br><span class="line">        <span class="comment">// 并将这些参数设置到DispatcherServlet中</span></span><br><span class="line">        PropertyValues pvs = <span class="keyword">new</span> HttpServletBean.ServletConfigPropertyValues(<span class="keyword">this</span>.getServletConfig(), <span class="keyword">this</span>.requiredProperties);</span><br><span class="line">        <span class="keyword">if</span>(!pvs.isEmpty()) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(<span class="keyword">this</span>);</span><br><span class="line">                <span class="comment">//获取服务器的各种信息</span></span><br><span class="line">                ResourceLoader resourceLoader = <span class="keyword">new</span> ServletContextResourceLoader(<span class="keyword">this</span>.getServletContext());</span><br><span class="line">                bw.registerCustomEditor(Resource.class, <span class="keyword">new</span> ResourceEditor(resourceLoader, <span class="keyword">this</span>.getEnvironment()));</span><br><span class="line">                <span class="comment">//模板方法，可以在子类中调用，做一些初始化工作，bw代表DispatcherServelt</span></span><br><span class="line">                <span class="keyword">this</span>.initBeanWrapper(bw);</span><br><span class="line">                <span class="comment">//将配置的初始化值设置到DispatcherServlet中</span></span><br><span class="line">                bw.setPropertyValues(pvs, <span class="keyword">true</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (BeansException var4) &#123;</span><br><span class="line">                <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isErrorEnabled()) &#123;</span><br><span class="line">                    <span class="keyword">this</span>.logger.error(<span class="string">"Failed to set bean properties on servlet '"</span> + <span class="keyword">this</span>.getServletName() + <span class="string">"'"</span>, var4);</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">throw</span> var4;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//模板方法，子类初始化的入口方法。此方法在HttpServletBean本身是空的，但是因为调用方法的对象是DispatcherServlet</span></span><br><span class="line">        <span class="comment">//所以优先在DispatcherServlet找，找不到再去父类找，最后在FrameworkServlet找到</span></span><br><span class="line">        <span class="keyword">this</span>.initServletBean();</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isDebugEnabled()) &#123;</span><br><span class="line">            <span class="keyword">this</span>.logger.debug(<span class="string">"Servlet '"</span> + <span class="keyword">this</span>.getServletName() + <span class="string">"' configured successfully"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">   ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h6 id="小结HttpServletBean："><a href="#小结HttpServletBean：" class="headerlink" title="小结HttpServletBean："></a>小结HttpServletBean：</h6><ul>
<li>获取web.xml的中配置DispatcherServlet的初始化参数，存放到一个参数容器ServletConfigPropertyValues中；</li>
<li>根据传进来的this创建对象包 裹者（BeanWrapper），本质上它就是DispatcherServlet；</li>
<li>最后通过bw.setPropertyValues(pvs,true);把参数设置到bw(即DispatcherServlet)里面去，最后调用子类的initServletBean()。</li>
</ul>
<hr>
<ul>
<li style="list-style: none"><input type="checkbox" checked> FrameworkServlet </li>
<li>由于篇幅过长，无关紧要的方法我就省略了 不做解释</li>
<li>可以先关注initServletBean 方法: 其实就是两行代码 主要进行初始化上下文对象和FrameworkServlet<blockquote>
<ul>
<li>//初始化 webApplicationContext <br></li>
<li>this.webApplicationContext = this.initWebApplicationContext();<br></li>
<li>this.initFrameworkServlet();<br></li>
</ul>
</blockquote>
</li>
<li>然后查看initWebApplicationContext() 方法：该方法做了三件事情<blockquote>
<ul>
<li>获取 Spring 的根容器 rootContext</li>
<li>设置 webApplicationContext 并根据情况调用 onRefresh 方法</li>
<li>将 webApplicationContext 设置到 ServletContext 中</li>
</ul>
</blockquote>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">FrameworkServlet</span> <span class="keyword">extends</span> <span class="title">HttpServletBean</span> <span class="keyword">implements</span> <span class="title">ApplicationContextAware</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title">initServletBean</span><span class="params">()</span> <span class="keyword">throws</span> ServletException </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.getServletContext().log(<span class="string">"Initializing Spring FrameworkServlet '"</span> + <span class="keyword">this</span>.getServletName() + <span class="string">"'"</span>);</span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isInfoEnabled()) &#123;</span><br><span class="line">            <span class="keyword">this</span>.logger.info(<span class="string">"FrameworkServlet '"</span> + <span class="keyword">this</span>.getServletName() + <span class="string">"': initialization started"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">long</span> startTime = System.currentTimeMillis();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 初始化 webApplicationContext，即创建springmvc的ioc容器实例</span></span><br><span class="line">            <span class="comment">// 下面我会对该方法进行详细说明</span></span><br><span class="line">            <span class="keyword">this</span>.webApplicationContext = <span class="keyword">this</span>.initWebApplicationContext();</span><br><span class="line">            <span class="comment">//模板方法</span></span><br><span class="line">            <span class="keyword">this</span>.initFrameworkServlet();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ServletException var5) &#123;</span><br><span class="line">            <span class="keyword">this</span>.logger.error(<span class="string">"Context initialization failed"</span>, var5);</span><br><span class="line">            <span class="keyword">throw</span> var5;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (RuntimeException var6) &#123;</span><br><span class="line">            <span class="keyword">this</span>.logger.error(<span class="string">"Context initialization failed"</span>, var6);</span><br><span class="line">            <span class="keyword">throw</span> var6;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isInfoEnabled()) &#123;</span><br><span class="line">            <span class="keyword">long</span> elapsedTime = System.currentTimeMillis() - startTime;</span><br><span class="line">            <span class="keyword">this</span>.logger.info(<span class="string">"FrameworkServlet '"</span> + <span class="keyword">this</span>.getServletName() + <span class="string">"': initialization completed in "</span> + elapsedTime + <span class="string">" ms"</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * initWebApplicationContext 方法做了三件事：</span></span><br><span class="line"><span class="comment">     * 获取 Spring 的根容器 rootContext</span></span><br><span class="line"><span class="comment">     * 设置 webApplicationContext 并根据情况调用 onRefresh 方法</span></span><br><span class="line"><span class="comment">     * 将 webApplicationContext 设置到 ServletContext 中</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> WebApplicationContext <span class="title">initWebApplicationContext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 获取 rootContext 首先通过ServletContext获得spring容器，因为子容器springMVC要和父容器spring容器进行关联</span></span><br><span class="line">        WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(<span class="keyword">this</span>.getServletContext());</span><br><span class="line">        <span class="comment">// 声明springMVC容器wac</span></span><br><span class="line">        WebApplicationContext wac = <span class="keyword">null</span>;</span><br><span class="line">        <span class="comment">// 判断容器是否已经存在了容器实例,存在的话直接赋值给wac，给springMVC容器设置父容器</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.webApplicationContext != <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 上下文实例在构造时注入 - &gt;使用它</span></span><br><span class="line">            wac = <span class="keyword">this</span>.webApplicationContext;</span><br><span class="line">            <span class="keyword">if</span>(wac <span class="keyword">instanceof</span> ConfigurableWebApplicationContext) &#123;</span><br><span class="line">                ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)wac;</span><br><span class="line">                <span class="keyword">if</span>(!cwac.isActive()) &#123;</span><br><span class="line">                    <span class="comment">// 如果上下文尚未刷新 -&gt; 提供诸如设置父上下文，设置应用程序上下文ID等服务</span></span><br><span class="line">                    <span class="keyword">if</span>(cwac.getParent() == <span class="keyword">null</span>) &#123;</span><br><span class="line">                        <span class="comment">// 上下文实例被注入没有显式的父类 -&gt; 将根应用程序上下文（如果有的话可能为null）设置为父级</span></span><br><span class="line">                        cwac.setParent(rootContext);</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="comment">// 最后调用刷新函数configureAndRefreshWebApplicationContext(wac)，作用是把springMVC.xml的配置信息加载到容器中去</span></span><br><span class="line">                    <span class="keyword">this</span>.configureAndRefreshWebApplicationContext(cwac);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 当 WebApplicationContext 已经存在 ServletContext 中时，通过配置在 servlet 中的 ContextAttribute 参数获取</span></span><br><span class="line">        <span class="keyword">if</span>(wac == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 在ServletContext中寻找是否有springMVC容器，初次运行是没有的</span></span><br><span class="line">            wac = <span class="keyword">this</span>.findWebApplicationContext();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 如果 WebApplicationContext 还没有创建，则创建一个</span></span><br><span class="line">        <span class="keyword">if</span>(wac == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="comment">// 创建springMVC容器 </span></span><br><span class="line">            wac = <span class="keyword">this</span>.createWebApplicationContext(rootContext);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 到这里mvc的容器已经创建完毕，接着才是真正调用DispatcherServlet的初始化方法onRefresh(wac)</span></span><br><span class="line">        <span class="keyword">if</span>(!<span class="keyword">this</span>.refreshEventReceived) &#123;</span><br><span class="line">            <span class="keyword">this</span>.onRefresh(wac);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.publishContext) &#123;</span><br><span class="line">            <span class="comment">// 将springMVC容器存放到ServletContext中去，方便下次取出来</span></span><br><span class="line">            String attrName = <span class="keyword">this</span>.getServletContextAttributeName();</span><br><span class="line">            <span class="keyword">this</span>.getServletContext().setAttribute(attrName, wac);</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isDebugEnabled()) &#123;</span><br><span class="line">                <span class="keyword">this</span>.logger.debug(<span class="string">"Published WebApplicationContext of servlet '"</span> + <span class="keyword">this</span>.getServletName() + <span class="string">"' as ServletContext attribute with name ["</span> + attrName + <span class="string">"]"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wac;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">protected</span> WebApplicationContext <span class="title">findWebApplicationContext</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String attrName = getContextAttribute();</span><br><span class="line">        <span class="keyword">if</span> (attrName == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        WebApplicationContext wac =</span><br><span class="line">                WebApplicationContextUtils.getWebApplicationContext(getServletContext(), attrName);</span><br><span class="line">        <span class="keyword">if</span> (wac == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> IllegalStateException(<span class="string">"No WebApplicationContext found: initializer not registered?"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> wac;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">protected</span> WebApplicationContext <span class="title">createWebApplicationContext</span><span class="params">(@Nullable ApplicationContext parent)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 获取创建类型</span></span><br><span class="line">        Class&lt;?&gt; contextClass = getContextClass();</span><br><span class="line">        <span class="comment">//删除了打印日志代码</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 检查创建类型</span></span><br><span class="line">        <span class="keyword">if</span> (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> ApplicationContextException(</span><br><span class="line">                    <span class="string">"Fatal initialization error in servlet with name '"</span> + getServletName() +</span><br><span class="line">                    <span class="string">"': custom WebApplicationContext class ["</span> + contextClass.getName() +</span><br><span class="line">                    <span class="string">"] is not of type ConfigurableWebApplicationContext"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 具体创建 实例化空白的ioc容器</span></span><br><span class="line">        ConfigurableWebApplicationContext wac =</span><br><span class="line">                (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);</span><br><span class="line">        <span class="comment">// 给容器设置环境</span></span><br><span class="line">        wac.setEnvironment(getEnvironment());</span><br><span class="line">        <span class="comment">// 给容器设置父容器(就是spring容器)，两个ioc容器关联在一起了</span></span><br><span class="line">        wac.setParent(parent);</span><br><span class="line">        <span class="comment">// 设置的 contextConfigLocation 参数传给 wac，默认是 WEB-INF/[ServletName]-Servlet.xml</span></span><br><span class="line">        <span class="comment">// 加载springMVC.xml的配置信息，bean注入，注解，扫描等等</span></span><br><span class="line">        wac.setConfigLocation(getContextConfigLocation());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 刷新容器，根据springMVC.xml配置文件完成初始化操作，此时springMVC容器创建完成</span></span><br><span class="line">        configureAndRefreshWebApplicationContext(wac);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> wac;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">configureAndRefreshWebApplicationContext</span><span class="params">(ConfigurableWebApplicationContext wac)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (ObjectUtils.identityToString(wac).equals(wac.getId())) &#123;</span><br><span class="line">            <span class="comment">// The application context id is still set to its original default value</span></span><br><span class="line">            <span class="comment">// -&gt; assign a more useful id based on available information</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>.contextId != <span class="keyword">null</span>) &#123;</span><br><span class="line">                wac.setId(<span class="keyword">this</span>.contextId);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">// Generate default id...</span></span><br><span class="line">                wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +</span><br><span class="line">                        ObjectUtils.getDisplayString(getServletContext().getContextPath()) + <span class="string">'/'</span> + getServletName());</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        wac.setServletContext(getServletContext());</span><br><span class="line">        wac.setServletConfig(getServletConfig());</span><br><span class="line">        wac.setNamespace(getNamespace());</span><br><span class="line">        <span class="comment">//此处</span></span><br><span class="line">        wac.addApplicationListener(<span class="keyword">new</span> SourceFilteringListener(wac, <span class="keyword">new</span> ContextRefreshListener()));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// The wac environment's #initPropertySources will be called in any case when the context</span></span><br><span class="line">        <span class="comment">// is refreshed; do it eagerly here to ensure servlet property sources are in place for</span></span><br><span class="line">        <span class="comment">// use in any post-processing or initialization that occurs below prior to #refresh</span></span><br><span class="line">        ConfigurableEnvironment env = wac.getEnvironment();</span><br><span class="line">        <span class="keyword">if</span> (env <span class="keyword">instanceof</span> ConfigurableWebEnvironment) &#123;</span><br><span class="line">            ((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        postProcessWebApplicationContext(wac);</span><br><span class="line">        applyInitializers(wac);</span><br><span class="line">        <span class="comment">//Spring的refresh()方法调用 Spring所有的初始化都在这个方法中完成</span></span><br><span class="line">        wac.refresh();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="小结FrameworkServlet："><a href="#小结FrameworkServlet：" class="headerlink" title="小结FrameworkServlet："></a>小结FrameworkServlet：</h5><ul>
<li>创建springMVC的ioc容器根据配置文件实例化里面各种bean，并将之与spring的ioc容器进行关联</li>
<li>把创建出来的mvc容器存放到ServletContext中</li>
<li>最后调用模板方法this.onRefresh(wac); 调用DispatcherServlet的初始化方法onRefresh(wac)</li>
</ul>
<hr>
<ul>
<li style="list-style: none"><input type="checkbox" checked> DispatcherServlet</li>
<li>DispatcherServlet 继承自 FrameworkServlet，onRefresh 方法是 DispatcherServlet 的入口方法，在 initStrategies 方法中调用了 9 个初始化的方法。</li>
<li>对这九个方法我进行了注解说明；</li>
<li>这里我们主要分析initLocaleResolver 方法</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DispatcherServlet</span> <span class="keyword">extends</span> <span class="title">FrameworkServlet</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onRefresh</span><span class="params">(ApplicationContext context)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 里面就是调用了initStrategies 方法 初始化一些特殊的bean</span></span><br><span class="line">        <span class="keyword">this</span>.initStrategies(context);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">initStrategies</span><span class="params">(ApplicationContext context)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 文件上传解析</span></span><br><span class="line">        <span class="keyword">this</span>.initMultipartResolver(context);</span><br><span class="line">        <span class="comment">// 本地化解析</span></span><br><span class="line">        <span class="keyword">this</span>.initLocaleResolver(context);</span><br><span class="line">        <span class="comment">// 主题解析</span></span><br><span class="line">        <span class="keyword">this</span>.initThemeResolver(context);</span><br><span class="line">        <span class="comment">// url请求映射</span></span><br><span class="line">        <span class="keyword">this</span>.initHandlerMappings(context);</span><br><span class="line">        <span class="comment">// 初始化真正调用controloler方法的类</span></span><br><span class="line">        <span class="keyword">this</span>.initHandlerAdapters(context);</span><br><span class="line">        <span class="comment">// 异常解析</span></span><br><span class="line">        <span class="keyword">this</span>.initHandlerExceptionResolvers(context);</span><br><span class="line">        <span class="comment">// 当处理器没有返回逻辑视图名等相关信息时，自动将请求URL映射为逻辑视图名；</span></span><br><span class="line">        <span class="keyword">this</span>.initRequestToViewNameTranslator(context);</span><br><span class="line">        <span class="comment">// 视图解析 ViewResolver将把逻辑视图名解析为具体的View，通过这种策略模式，很容易更换其他视图技术；如InternalResourceViewResolver将逻辑视图名映射为jsp视图</span></span><br><span class="line">        <span class="keyword">this</span>.initViewResolvers(context);</span><br><span class="line">        <span class="comment">//用于管理FlashMap的策略接口，FlashMap用于存储一个请求的输出，当进入另一个请求时作为该请求的输入，通常用于重定向场景</span></span><br><span class="line">        <span class="keyword">this</span>.initFlashMapManager(context);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initLocaleResolver</span><span class="params">(ApplicationContext context)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 通过context获取LocaleResolver</span></span><br><span class="line">            <span class="keyword">this</span>.localeResolver = (LocaleResolver)context.getBean(<span class="string">"localeResolver"</span>, LocaleResolver.class);</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isDebugEnabled()) &#123;</span><br><span class="line">                <span class="keyword">this</span>.logger.debug(<span class="string">"Using LocaleResolver ["</span> + <span class="keyword">this</span>.localeResolver + <span class="string">"]"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (NoSuchBeanDefinitionException var3) &#123;</span><br><span class="line">            <span class="comment">// 使用默认策略</span></span><br><span class="line">            <span class="keyword">this</span>.localeResolver = (LocaleResolver)<span class="keyword">this</span>.getDefaultStrategy(context, LocaleResolver.class);</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.logger.isDebugEnabled()) &#123;</span><br><span class="line">                <span class="keyword">this</span>.logger.debug(<span class="string">"Unable to locate LocaleResolver with name 'localeResolver': using default ["</span> + <span class="keyword">this</span>.localeResolver + <span class="string">"]"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">protected</span> &lt;T&gt; <span class="function">T <span class="title">getDefaultStrategy</span><span class="params">(ApplicationContext context, Class&lt;T&gt; strategyInterface)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 调用 getDefaultStrategies 方法</span></span><br><span class="line">        List&lt;T&gt; strategies = <span class="keyword">this</span>.getDefaultStrategies(context, strategyInterface);</span><br><span class="line">        <span class="keyword">if</span>(strategies.size() != <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> BeanInitializationException(<span class="string">"DispatcherServlet needs exactly 1 strategy for interface ["</span> + strategyInterface.getName() + <span class="string">"]"</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> strategies.get(<span class="number">0</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">protected</span> &lt;T&gt; <span class="function">List&lt;T&gt; <span class="title">getDefaultStrategies</span><span class="params">(ApplicationContext context, Class&lt;T&gt; strategyInterface)</span> </span>&#123;</span><br><span class="line">        String key = strategyInterface.getName();</span><br><span class="line">        <span class="comment">// 根据策略接口的名字从 defaultStrategies 获取所需策略的类型</span></span><br><span class="line">        String value = defaultStrategies.getProperty(key);</span><br><span class="line">        <span class="keyword">if</span>(value == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> LinkedList();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 如果有多个默认值的话，就以逗号分隔为数组</span></span><br><span class="line">            String[] classNames = StringUtils.commaDelimitedListToStringArray(value);</span><br><span class="line">            List&lt;T&gt; strategies = <span class="keyword">new</span> ArrayList(classNames.length);</span><br><span class="line">            String[] var7 = classNames;</span><br><span class="line">            <span class="keyword">int</span> var8 = classNames.length;</span><br><span class="line">            <span class="comment">// 按获取到的类型初始化策略</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">int</span> var9 = <span class="number">0</span>; var9 &lt; var8; ++var9) &#123;</span><br><span class="line">                String className = var7[var9];</span><br><span class="line"></span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    Class&lt;?&gt; clazz = ClassUtils.forName(className, DispatcherServlet.class.getClassLoader());</span><br><span class="line">                    Object strategy = <span class="keyword">this</span>.createDefaultStrategy(context, clazz);</span><br><span class="line">                    strategies.add(strategy);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (ClassNotFoundException var13) &#123;</span><br><span class="line">                    <span class="keyword">throw</span> <span class="keyword">new</span> BeanInitializationException(<span class="string">"Could not find DispatcherServlet's default strategy class ["</span> + className + <span class="string">"] for interface ["</span> + key + <span class="string">"]"</span>, var13);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (LinkageError var14) &#123;</span><br><span class="line">                    <span class="keyword">throw</span> <span class="keyword">new</span> BeanInitializationException(<span class="string">"Error loading DispatcherServlet's default strategy class ["</span> + className + <span class="string">"] for interface ["</span> + key + <span class="string">"]: problem with class file or dependent class"</span>, var14);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> strategies;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="小结DispatcherServlet"><a href="#小结DispatcherServlet" class="headerlink" title="小结DispatcherServlet"></a>小结DispatcherServlet</h5><ul>
<li>Spring MVC 自身创建过程，分析了 Spring MVC 中 Servlet 的三个层次：HttpServletBean、FrameworkServlet 和 DispatcherServlet。HttpServletBean 继承自 Java 的 HttpServlet，其作用是将配置的参数设置到相应的属性上；FrameworkServlet 初始化了 WebApplicationContext；DispatcherServlet 初始化了自身的 9 个组件。</li>
</ul>
<hr>
</the>
      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/">3.SpringMVC -初始化源码分析</a></p>
        <p><span>文章作者:</span><a href="/" title="回到主页">zhengweiMao</a></p>
        <p><span>发布时间:</span>2018-07-12, 15:38:05</p>
        <p><span>最后更新:</span>2018-08-06, 23:22:03</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/" title="3.SpringMVC -初始化源码分析">https://maozw.gitee.io/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/</a>
            <span class="copy-path" data-clipboard-text="原文: https://maozw.gitee.io/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/　　作者: zhengweiMao" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/04.SpringMVC-----源码分析与实践--DispatcherServlet核心/">
                    4.SpringMVC -源码分析与实践-DispatcherServlet
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/02.SpringMVC-----源码分析与实践--环境搭建/">
                    2.SpringMVC -源码分析与实践--环境搭建
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
        <strong class="toc-title">文章目录</strong>
        
            <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#SpringMVC—–源码分析与实践"><span class="toc-number">1.</span> <span class="toc-text">SpringMVC—–源码分析与实践</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#序言"><span class="toc-number">1.0.0.0.1.</span> <span class="toc-text">序言</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SpringMVC"><span class="toc-number">1.0.1.</span> <span class="toc-text">SpringMVC</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SpringMVC—–源码分析与实践-初始化分析"><span class="toc-number">1.1.</span> <span class="toc-text">SpringMVC—–源码分析与实践-初始化分析</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#一-启动分析"><span class="toc-number">2.</span> <span class="toc-text">一  启动分析</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Spring-MVC-核心-Servlet-架构图如下："><span class="toc-number">2.1.</span> <span class="toc-text">Spring MVC 核心 Servlet 架构图如下：</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#这里主要阐述Spring-中的-HttpServletBean、FrameworkServlet、DispatcherServlet-这三个类的创建过程。"><span class="toc-number">2.1.0.1.</span> <span class="toc-text">这里主要阐述Spring 中的 HttpServletBean、FrameworkServlet、DispatcherServlet 这三个类的创建过程。</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#注意：XXXService也是Spring所托管的bean。"><span class="toc-number">2.1.0.1.1.</span> <span class="toc-text">注意：XXXService也是Spring所托管的bean。</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#好了言归正传，说明了上面三个接口之后，该说我们的正主了；"><span class="toc-number">2.1.1.</span> <span class="toc-text">好了言归正传，说明了上面三个接口之后，该说我们的正主了；</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#小结HttpServletBean："><span class="toc-number">2.1.1.0.1.</span> <span class="toc-text">小结HttpServletBean：</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#小结FrameworkServlet："><span class="toc-number">2.1.1.1.</span> <span class="toc-text">小结FrameworkServlet：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#小结DispatcherServlet"><span class="toc-number">2.1.1.2.</span> <span class="toc-text">小结DispatcherServlet</span></a></li></ol></li></ol></li></ol></li></ol>
        
    </div>
    <style>
        .left-col .switch-btn,
        .left-col .switch-area {
            display: none;
        }
        .toc-level-3 i,
        .toc-level-3 ol {
            display: none !important;
        }
    </style>

    <input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">

    <script>
        yiliaConfig.toc = ["隐藏目录", "显示目录", !!"false"];
    </script>



    
<div class="share">
    
        <div class="bdsharebuttonbox">
            <a href="#" class="fa fa-twitter bds_twi" data-cmd="twi" title="分享到推特"></a>
            <a href="#" class="fa fa-weibo bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
            <a href="#" class="fa fa-qq bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
            <a href="#" class="fa fa-files-o bds_copy" data-cmd="copy" title="复制网址"></a>
            <a href="#" class="fa fa fa-envelope-o bds_mail" data-cmd="mail" title="通过邮件分享"></a>
            <a href="#" class="fa fa-weixin bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
            <a href="#" class="fa fa-share-alt bds_more" data-cmd="more"></i></a>
        </div>
        <script>
            window._bd_share_config={
                "common":{"bdSnsKey":{},"bdText":"3.SpringMVC -初始化源码分析　| 花神　","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
        </script>
    

    
</div>







    




    <div class="scroll" id="post-nav-button">
        
            <a href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/04.SpringMVC-----源码分析与实践--DispatcherServlet核心/" title="上一篇: 4.SpringMVC -源码分析与实践-DispatcherServlet">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/02.SpringMVC-----源码分析与实践--环境搭建/" title="下一篇: 2.SpringMVC -源码分析与实践--环境搭建">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/9.Spring/SpringBoot/SpringBoot-启动源码解析/">1.SpringBoot源码分析之SpringBoot的启动过程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/2.反射框架--reflections-抓取实例/">1.反射框架--reflections-抓取实例</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.服务器相关/1.服务器--Sigar 获取CPU和Memory内存等信息使用详解/">1.Sigar 获取CPU和Memory内存等信息使用详解</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/11.java-reflections/1.反射框架--reflections-基本介绍/">1.反射框架--reflections-基本介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/10.算法/算法----外部排序/">2.算法 -外部排序</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/10.算法/算法----snowflake/">1.算法 -snowflake</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/05/花神(maoge)/8.面试/面试/">1.面试</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/7.Quartz-Job状态与并发/">7.任务调度框架Quartz-Job状态与并发</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/6.任务调度框架Quartz-config/">5.任务调度框架Quartz-Quartz-Job Stores</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/5.任务调度框架Quartz-Job Stores/">5.任务调度框架Quartz-Quartz-Job Stores</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/4.任务调度框架Quartz-SchedulerListeners/">4.任务调度框架Quartz-SchedulerListeners</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/3.任务调度框架Quartz-TriggerListeners和JobListeners/">3.任务调度框架Quartz-Triggers-SimpleTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-3.任务调度框架Quartz-Triggers-CronTrigger/">2-2.任务调度框架Quartz-Triggers-CronTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-2.任务调度框架Quartz-Triggers-SimpleTrigger/">2-2.任务调度框架Quartz-Triggers-SimpleTrigger</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/2-1.任务调度框架Quartz-Triggers详解理解/">2-1.任务调度框Quartz-Triggers详解理解</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/08/01/花神(maoge)/6.quartz--定时任务框架/1.任务调度框架Quartz-入门/">1.任务调度框架Quartz-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/05.SpringMVC-----源码分析与实践--HandlerMapping(1)/">1.SpringMVC -源码分析与实践-HandlerMapping</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/04.SpringMVC-----源码分析与实践--DispatcherServlet核心/">4.SpringMVC -源码分析与实践-DispatcherServlet</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/03.SpringMVC-----源码分析与实践--初始化分析/">3.SpringMVC -初始化源码分析</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/02.SpringMVC-----源码分析与实践--环境搭建/">2.SpringMVC -源码分析与实践--环境搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/7.JAVA_WEB相关技术/1.基于AsyncRestTemplate异步HTTP请求的一种轻量级技术实现/">1.基于AsyncRestTemplate异步HTTP请求的一种轻量级技术实</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/12/花神(maoge)/9.Spring/SpringMVC--源码解读/01.SpringMVC-----源码分析与实践----初步认知/">1.SpringMVC -源码分析与实践----初步认知</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/20.并发编程--并发框架-Disruptor--应用示例3/">20.并发编程--并发框架-Disruptor--应用示例3</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/19.并发编程--并发框架-Disruptor--应用示例2/">19并发编程--并发框架-Disruptor--应用示例2</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/18.并发编程--并发框架-Disruptor--应用示例1/">18.并发编程--并发框架-Disruptor--应用示例1</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/17.并发编程--并发框架-Disruptor--术语介绍/">17.并发编程--并发框架-Disruptor--介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/16.并发编程--并发框架-Disruptor--入门介绍/">16.并发编程--并发框架-Disruptor--介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/15.并发编程--锁-介绍/">15.并发编程--锁-介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/14.并发编程--Concurrent-工具类/">14.并发编程--Concurrent-工具类介绍</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/13.并发编程--线程池介绍/">13. 并发编程--线程池</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/12.并发编程--多线程设计模式 - 生产者-消费者模式/">12并发编程\12.并发编程--多线程设计模式 - 生产者-消费者模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/11.并发编程--多线程设计模式 - Master-Worker模式/">11多线程设计模式 - Master-Worker模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/7.并发编程--单例模式(饥饿模式-懒汉模式)/">7.并发编程--单例模式(饥饿模式-懒汉模式)</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/10.并发编程--多线程设计模式 - Future模式/">10 多线程设计模式 - Future模式</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/8.并发编程--同步类容器和并发类容器/">8并发编程--同步类容器和并发类容器</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/6.并发编程--ThreadLocal/">6.并发编程--ThreadLocal</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/9.并发编程--Queue/">9并发编程--Queue</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/5.并发编程--多线程通信-wait-notify-模拟Queue/">并发编程--多线程通信-wait-notify-模拟Queue</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/4-2.并发编程--多线程通信-wait-notify-notifyAll-join/">4-2.并发编程--并发编程--多线程通信-wait-notify-notifyAll-join</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/4-1.并发编程--多线程通信-wait-notify/">4.并发编程--多线程通信-wait-notify</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/1.并发编程--线程基础/">并发编程--线程基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/3.并发编程--volatile 细节说明/">并发编程--volatile</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/2.并发编程-synchronized 细节说明/">并发编程-synchronized 细节说明</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/07/05/花神(maoge)/1.并发编程/0.并发编程-readMe/">并发编程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/0.并发编程-readMe/">网络编程</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/13.网络编程--Netty-文件的上传和下载/">13.网络编程--Netty-文件的上传和下载</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/14.网络编程--mina-入门/">14.网络编程--mina-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/12.网络编程--Netty-实践-心跳检测/">12.网络编程--Netty-实践-心跳检测</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/11.网络编程--Netty-实践-数据通信/">11.网络编程--Netty-实践-数据通信+心跳检测</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/10.网络编程--Netty-基础-WebSocket/">10.网络编程--Netty-基础-WebSocket</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/9.网络编程--Netty-基础-UDP/">9.网络编程--Netty-UDP</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/8.网络编程--Netty-基础-拆包粘包-2/">8.网络编程--Netty-拆包粘包</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/7.网络编程--Netty-基础-拆包粘包-1/">7.网络编程--Netty-拆包粘包</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/6.网络编程--Netty-基础-HelloWorld/">6.网络编程--Netty-HelloWorld</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/5.网络编程--Netty-基础-入门/">5.网络编程--Netty-入门</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/4.网络编程--基础-AIO异步实现/">4.网络编程--基础-AIO异步实现</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/3.网络编程--基础-NIO异步实现/">3.网络编程--基础-NIO异步实现</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/2.网络编程--基础-BIO伪异步实现/">网络编程--基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/05/05/花神(maoge)/2.网络编程/1.网络编程--基础-socket/">网络编程--基础</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/04/05/花神(maoge)/5.分布式--zookeeper/2.zookeeper--集群搭建/">2.分布式--zookeeper\2.zookeeper--集群搭建</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/04/05/花神(maoge)/5.分布式--zookeeper/1.zookeeper--简介/">1.分布式--zookeeper--zookeeper简介</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/4.JVM相关/1.jvm虚拟机组成部分/">1.jvm虚拟机组成部分</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/3.缓存-中间件/Redis 配置文件说明/">Redis集群搭建-配置文件</a></li><li class="post-list-item"><a class="post-list-link" href="/maoge-blog/2018/02/05/花神(maoge)/3.缓存-中间件/Redis----集群搭建/">Redis集群搭建-实践</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                <i class="fa fa-copyright"></i> 
                2016-2018 zhengweiMao
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减 Hexo 双栏博客主题  v3.5">Yelee</a> by MOxFIVE <i class="fa fa-heart animated infinite pulse"></i>
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" title="本站到访数"><i class="fa fa-user" aria-hidden="true"></i><span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>| </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit"  title="本页阅读量"><i class="fa fa-eye animated infinite pulse" aria-hidden="true"></i><span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/maoge-blog/js/main.js" src="//cdn.bootcss.com/require.js/2.2.0/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>





<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" onclick="load$hide();" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>
<script>
    // Open in New Window
    
        var oOpenInNew = {
            
            
            
            
            
            
             archives: ".archive-article-title", 
             miniArchives: "a.post-list-link", 
            
             friends: "#js-friends a", 
             socail: ".social a" 
        }
        for (var x in oOpenInNew) {
            $(oOpenInNew[x]).attr("target", "_blank");
        }
    
</script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>